package cn.meng.base.utils;

import cn.meng.base.vo.BusinessException;
import cn.meng.base.vo.EmployeeVo;
import cn.meng.base.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * 邮件发送工具类
 * @author 万梦辉
 */
@Component
public class SendMailUtil {

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    TemplateEngine templateEngine;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private MimeMessage msg;
    private MimeMessageHelper helper;

    /**
     * 发送邮件，不带有附件
     * @param employee
     * @throws BusinessException
     */
    public void sendMail(EmployeeVo employee) throws BusinessException {
        process(employee,null);
    }

    /**
     * 发送邮件、带有附件
     * @param employee
     * @param files
     * @throws BusinessException
     */
    public void sendMailWithAttachment(EmployeeVo employee,File[] files) throws BusinessException {
        process(employee,files);
    }

    /**
     * 根据是否包含文件去判断是否需要启动附件功能。
     * @param employee
     * @param files
     * @throws BusinessException
     */
    public void process(EmployeeVo employee,File[] files) throws BusinessException {
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        if(files != null) {
            try {
                helper = new MimeMessageHelper(msg,true);
                int temp = 1;
                for(File file : files) {
                    helper.addAttachment(file.getName(),file);
                    temp++;
                }
            } catch (MessagingException e) {
                log.error(e.getMessage());
                throw new BusinessException(500,"处理带有附件的邮件发送失败");
            }
        }
        try {
            helper.setTo(employee.getEmail());
            helper.setFrom(mailProperties.getUsername());
            helper.setSubject("入职欢迎");
            helper.setSentDate(new Date());
            Context context = new Context();
            context.setVariable("name", employee.getName());
            context.setVariable("posName", employee.getPosName());
            context.setVariable("joblevelName", employee.getJoblevelName());
            context.setVariable("departmentName", employee.getDepartmentName());
            String mail = templateEngine.process("mail", context);
            helper.setText(mail, true);
        } catch (MessagingException e) {
            log.error("邮件发送失败");
            throw new BusinessException(500,"邮件发送失败");
        }
        javaMailSender.send(msg);
        log.info("邮件发送成功");
    }

    public void forgetPassword(UserVo user) throws BusinessException {
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        try {
            helper.setTo(user.getEmail());
            helper.setFrom(mailProperties.getUsername());
            helper.setSubject("账号信息");
            helper.setSentDate(new Date());
            Context context = new Context();
            context.setVariable("username", user.getUsername());
            context.setVariable("password", user.getPassword());
            String mail = templateEngine.process("passwordEmail", context);
            helper.setText(mail, true);
        } catch (MessagingException e) {
            log.error("邮件发送失败");
            throw new BusinessException(500,"邮件发送失败");
        }
        javaMailSender.send(msg);
    }

}
